<template>
  <div class="home_game_hot_box">
    <div class="card_list_box">
      <ul class="card_title">
        <li class="title_left">
          <ImgT :key="props.tabInfo.icon_h5" :src="joinURL(settings.backend_upload, props.tabInfo.icon_h5)" alt="" />
          <span v-if="tabInfo == 3 || tabInfo == 4">{{ t('home_page_hot_event') }}</span>
          <span v-else>
            {{ t("home_page_hot_event_name", { name: unserialize(tabInfo.name, false) }) }}
          </span>
        </li>
      </ul>
    </div>
    <GameList :games="hotBoxGames" :type="tabInfo" />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, watch, onBeforeMount } from 'vue';
import GameList from "./gameList.vue";
import { unserialize } from '@/utils/useStoreMethods';
import { joinURL } from '@/utils/publicMethods'

import { NetPacket } from '@/netBase/NetPacket';
import PKwebsocket from "@/lolo/Ws";
import { storeToRefs } from 'pinia';
import pinia from '@/store/index';
import { Page } from '@/store/page';

const { hotBoxGames } = storeToRefs(Page(pinia));
const { settings } = storeToRefs(Page(pinia));
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

const props: any = defineProps({
  tabInfo: {
    type: Object,
    default: () => ({}), // 设置默认值
  },
});

console.log(unserialize(props.tabInfo.name, false), props.tabInfo)

const tabInfo = ref({ ...props.tabInfo });


const state = reactive({
  // 游戏平台id  -1为查看全部的游戏
  agentId: -1,

  // 自定义标签 0为全部 1为热门 -2为收藏，-3为最近
  lableId: 1,

  //也表示场馆id -1 为所有场馆
  kindIdHotBox: props.tabInfo.id,

  // 当前点击的游戏
  currentGame: null as any,
})

const params: any = reactive({ // 参数
  pageSize: 20,
  page: 1,
})
const result: any = ref({ // 结果
  total_page: 0,
  // 游戏集合
  list: []
})


const queryData = () => {
  let ishotGames: any = []
  props.tabInfo.three_platform.flatMap((item: any) => {
    if (item.three_game_kind) {
      item.three_game_kind.flatMap((kind: any) => {

        kind.three_game.filter((game: any) => {
          // console.log(game, "game>>>>>>>>>")
          if (game.is_hot === 1) {
            ishotGames.push(game)
            Page(pinia).updateHotBoxGames(ishotGames);
          }
        })
      }

      )
    }
  });
  console.log(ishotGames, "ishotGames>>>>>>>>>>")
}

const resetData = () => {
  params.page = 1
  result.list = []
}

const handleEventsList = async (res: any) => {
  await Page(pinia).updateHotBoxGames(res.tournm_list);
};

const queryHotEventsData = () => {
  const eventsList = NetPacket.req_tournament_events_list();
  eventsList.game_type = 0;
  eventsList.page = 20;
  PKwebsocket.instance.send(eventsList, true, {
    callbackName: 'msg_notify_tournament_events_list',
    callback: handleEventsList
  })
}

watch(
  () => props.tabInfo.id,
  (newId) => {
    tabInfo.value = newId
    state.kindIdHotBox = newId;
    setTimeout(() => {
      resetData(); // 清空列表数据
      if (newId === 3 || newId === 4) {
        queryHotEventsData();
      } else {
        queryData()
      }
    }, 300)
  },
  { immediate: true }
);

onBeforeMount(() => {
  if (tabInfo.value.id === 3 || tabInfo.value.id === 4) {
    queryHotEventsData();
  } else if (tabInfo.value === 1) {
    queryData()
  }
});


</script>

<style lang="scss" scoped>
.home_game_hot_box {

  .card_list_box {
    padding-left: 14px;
    margin: 0 auto;

    .card_title {
      display: flex;
      padding: 0 14px 0 0;
      justify-content: space-between;
      align-items: center;
      height: 24px;
      line-height: 24px;
      margin: 12px 0;

      .title_left {
        font-family: PingFang SC;
        font-size: 14px;
        font-weight: 600;
        line-height: 16px;
        text-align: left;
        color: #FCFFFA;
        display: flex;
        justify-content: space-between;
        align-items: center;

        img {
          width: 18px;
          height: 18px;
          margin-right: 5px;
        }
      }

      .title_right {
        font-family: PingFang SC;
        font-size: 12px;
        font-weight: 500;
        line-height: 12px;
        text-align: left;
        color: #C6C4F5;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .more {
          background: #282747;
          margin-left: 8px;
          padding: 8px;
          border-radius: 5px;
          box-shadow: 0.5px 0.5px 1px 0px #4B4A6E inset;
        }
      }
    }
  }

  .hot_game_list {
    width: 347px;
    margin: 0 auto;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1px;

    .all_game {
      width: 82px;
      height: 108px;
      margin-right: 6px;
      margin-bottom: 6px;
      background: #1A1944;
      border-radius: 8px;
      position: relative;

      .bg_img {
        width: 82px;
        height: 82px;
      }

      .game_name {
        width: 100%;
        height: 26px;
        text-align: center;
        transform: translateY(-6px);
        color: #fff;
        font-size: 12px;
        overflow: hidden;
        word-break: break-word;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
      }

      .collect_img {
        position: absolute;
        top: 4px;
        right: 4px;
      }

      .masking_layer {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        backdrop-filter: blur(1px);
        background: rgba(0, 0, 0, 0.6);
        font-family: PingFang SC;
        font-size: 12px;
        font-weight: 500;
        line-height: 18px;
        color: #ffffff;

        .maskin_img {
          width: 32px;
          height: 32px;
          margin-bottom: 8px;
        }
      }
    }
  }
}
</style>